import { createSlice } from '@reduxjs/toolkit'
import { Toast } from 'antd-mobile'

export const PerSlice = createSlice({
  name: 'PerSlice',
  initialState: {
    PersonList:[
        {
            id:1,
            name:'迪丽热巴',
            state:false,
            code:'164579800651487201'
        },
        {
            id:2,
            name:'杨颖',
            state:false,
            code:'468510006518974516'
        },
        {
            id:3,
            name:'阿凡提',
            state:false,
            code:'648515400354798516'
        },
        {
            id:4,
            name:'神兵小将',
            state:false,
            code:'156584897006515031'
        },
    ]
  },
  reducers: {
    SwitchPer(state,{payload}){
        const item=state.PersonList.find((item)=>item.id===payload.id)
        item.state=!item.state
    },
    DelPer(state,{payload}){
        state.PersonList=state.PersonList.filter((item)=>item.id!==payload.id)
    },
    AddPer(state,{payload}){
        const item=state.PersonList.find((item)=>item.id===payload.id)
        if(!item){
            Toast.show('新增成功')
            state.PersonList.push(payload)
        }
        else{
            Toast.show('此人已存在')
        }
    },
    EditPer(state,{payload}){
        const item=state.PersonList.find((item)=>item.id===payload.id)
        if(item){
            Toast.show('编辑成功')
            Object.assign(item,payload)
        }
        else{
            Toast.show('注意格式，编辑失败')
        }
    },
  },
})

export const { SwitchPer,DelPer,AddPer,EditPer } = PerSlice.actions

export default PerSlice.reducer